--[[
  浙商银行核心交易Trade协议解析
]]
require("apm_core");
require("ApmXml");

local G_DEBUG = false;

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local t_xml_tree;

--- 检测是否属于Trade协议
-- return
-- true; false
local function trade_detect(s_payload)
    if s_payload == nil then return false end;
    
    t_xml_tree = ApmXml.load(s_payload);

    return ApmXml.getValue(t_xml_tree, "/Trade") ~= nil;
end

--- 解析请求
-- return
-- true: 解析成功; false: 解析失败
local function trade_parse_request(ApmData)
    local s_text;

    s_text = ApmXml.getValue(t_xml_tree, "/Trade/RequestHeader/cmdType/text()");
    if s_text ~= nil then
--        logInfo("/Trade/RequestHeader/cmdType:[%s]", s_text);
    end

    s_text = ApmXml.getValue(t_xml_tree, "/Trade/RequestHeader/requester/text()");
    if s_text ~= nil then
--        logInfo("/Trade/RequestHeader/requester:[%s]", s_text);
        -- XXX: 直接只是取了requester字段当作title,随便设的
        ApmData.replaceField("transaction.title", s_text);
    end

    s_text = ApmXml.getValue(t_xml_tree, "/Trade/RequestHeader/channel/text()");
    if s_text ~= nil then
--        logInfo("/Trade/RequestHeader/channel:[%s]", s_text);
        ApmData.replaceField("transaction.channel_code", s_text);
    end

    return true;
end

--- 解析响应
-- return
-- true: 解析成功; false: 解析失败
local function trade_parse_response(ApmData)
    local s_text;

    s_text = ApmXml.getValue(t_xml_tree, "/Trade/ResponseHeader/errorNo/text()");
    if s_text ~= nil then
        logInfo("/Trade/ResponseHeader/errorNo:[%s]", s_text);
        ApmData.replaceField("response.code", s_text == "000000" and 0 or 1);
        ApmData.replaceField("response.code_string", s_text);
    end

    s_text = ApmXml.getValue(t_xml_tree, "/Trade/ResponseHeader/errorInfo/text()");
    if s_text ~= nil then
        logInfo("/Trade/ResponseHeader/errorInfo:[%s]", s_text);
        ApmData.replaceField("response.error_message", s_text);
    end

    return true;
end

--- 二代支付协议解析入口函数
-- return
function post_analysis()
    local ApmData = require("ApmData");
    -- 父协议不是cicsts的退出
    if ApmData.getL7Proto() ~= 1435 then return end

    local s_commarea, s_struct_type, s_mesg_type, s_mesg_id, s_mesg_ref_id;
    local s_em_type = ApmData.getMsgType();
    if s_em_type == nil then
        return;
    end

    if s_em_type == 0 then -- 请求
        s_commarea = ApmData.getField("request.dts.service.arg.1", 1);
        if trade_detect(s_commarea) then
--            logInfo("commarea:[%s]", s_commarea);
            trade_parse_request(ApmData);
        end
    elseif s_em_type == 1 then -- 响应
        s_commarea = ApmData.getField("request.dts.service.arg.1", 1);
        if trade_detect(s_commarea) then
            --            logInfo("commarea:[%s]", s_commarea);
            trade_parse_request(ApmData);
        end

        s_commarea = ApmData.getField("response.dts.service.return.1", 1);
        if trade_detect(s_commarea) then
--            logInfo("commarea:[%s]", s_commarea);
            trade_parse_request(ApmData);
            trade_parse_response(ApmData);
        end
    end

    return;
end
